/****************************************************************************
 * Copyright 2010 kraigs.android@gmail.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 ****************************************************************************/

package bfa.playlist_importer_exporter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * This adapter is used to query the alarm database and translate each alarm
 * into a view which is displayed in a ListView.
 */
public final class PlaylistViewAdapter extends ArrayAdapter<Playlist> {
  private LayoutInflater inflater;

  
  public PlaylistViewAdapter(Activity activity,ArrayList<Playlist> res) {
    super(activity, 0, res);
   this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }



  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = inflater.inflate(R.layout.list_item, null);
    TextView labelView = (TextView) view.findViewById(R.id.alarm_label);
    CheckBox enabledView = (CheckBox) view.findViewById(R.id.alarm_enabled);
    

    final Playlist playlist = getItem(position);

    
    labelView.setText(playlist.name);
    enabledView.setChecked(playlist.selected);


    enabledView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        CheckBox check = (CheckBox) v;
        playlist.selected=check.isChecked();
      }
    });
    return view;
  }
}
